package com.steven.weather;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpClient {
	 public static void main(String[] args) {
		 
		 String city ="徐州";
	
		String	key=null;
		 @SuppressWarnings("deprecation")
		String url="http://www.nmc.gov.cn//search_result.php?keys=" ;
		try {
			 	key = URLEncoder.encode(city,"gbk");
			 	url +=key;
			
			 	
				
			try {
				URL u = new URL(url);
				URLConnection uc = 	u.openConnection();
				
			InputStream in = 	uc.getInputStream();
			BufferedReader bin = new BufferedReader(new InputStreamReader(in,"gbk") );
			String line =null;
			StringBuilder sb = new StringBuilder();
			while((line = bin.readLine())!=null){
				sb.append(line);
				
				
				
			}
			System.out.println(sb.toString());
	 
			 
		//	ParaseHtml(sb.toString());
		
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 	
			 	
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
	 
	    public static  void ParaseHtml(String html)

	    {

	 

	       String climate = ParaseWeatherInfo(html,"天气");

	       String temperature = ParaseWeatherInfo(html,"气温");

	       String wind = ParaseWeatherInfo(html,"风");

	       String barometric = ParaseWeatherInfo(html,"气压");

	      

	    System.out.println("天气："+climate +"/ 气温 ："+temperature +"/ 风 "+wind +"/ 气压"+barometric);

	    }

	    public static String ParaseWeatherInfo(String str,String word)

	    {

	       String w = null;

	       String st = "w365line1";

	       //System.err.println(str);

	       int a = str.indexOf(st);
	       System.out.println(a);

	       if(a!= -1)

	       {

	           String div1 = "<div";

	           String div2 = "</div>";

	           String keyword = ":";

	           int d1 = str.lastIndexOf(div1, a);

	           int d2 = str.indexOf(div2, d1);

	           	System.out.println(d1);
	           	System.out.println(d2);
	           if(d2 != -1)

	           {

	              String str4 = null;

	              String str2 = str.substring(d1, d2+div2.length());
	              System.out.println(str2);

	              if(str2.indexOf(word) !=-1)

	              {

	                  str4 = filerStr(str2,word,"");

	                  int k1 = str4.indexOf(keyword);

	                  str4 = str4.substring(k1+1,str4.length()).trim();

	                  return str4;

	              }  

	              else

	              {

	                  String s5 = str.replace(str2, "");

	                  w = ParaseWeatherInfo(s5,word);

	                  return w;

	              }

	           }

	       }

	      

	       return w;

	    }

	    public  static String filerStr(String html,String word,String replacement)

	    {   

	    try{

	        if(replacement==null)

	            replacement = "";

	        String   str=html; 

	            String   str1="(<[^>]*>)|(&nbsp;)"; 

	            Pattern   p=Pattern.compile(str1); 

	            Matcher   m=p.matcher(str); 

	            boolean   f=m.find(); 

	            StringBuffer   sb=new   StringBuffer();  

	            while(f)   { 

	           

	                m.appendReplacement(sb,replacement); 

	                f=m.find(); 

	            } 

	            m.appendTail(sb);

	           

	            return sb.toString();

	       

	    }

	    catch(Exception e)

	    {

	       

	    }

	   

	    return html;

	    }
}
